//
//  ZSY_HomeListTableViewCell.m
//  zsy_BreadTrip
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZSY_HomeListTableViewCell.h"
#import "ZSY_HomeListCollectionViewcell.h"

@interface ZSY_HomeListTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) ZSY_HomeListCollectionViewcell *cell;
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) ZSY_CollectionView *homeCollectionView;

@end

@implementation ZSY_HomeListTableViewCell

- (void)dealloc {
    [_homeCollectionView release];
    [_cell release];
    [_collectionArray release];
    [_flowLayout release];
    [super dealloc];
}

- (void)setCollectionArray:(NSMutableArray *)collectionArray {
    if (_collectionArray != collectionArray) {
        [_collectionArray release];
        _collectionArray = [collectionArray retain];
    }
    self.tableArray = collectionArray;
    [self.homeCollectionView reloadData];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews {
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake((WIDTH - 30) / 2, 200);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.homeCollectionView = [[ZSY_CollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 440) collectionViewLayout:_flowLayout];
    self.homeCollectionView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:243 / 255.0 blue:233 / 255.0 alpha:0];
    self.homeCollectionView.delegate = self;
    self.homeCollectionView.dataSource = self;
    [self.contentView addSubview:_homeCollectionView];
    [self.homeCollectionView registerClass:[ZSY_HomeListCollectionViewcell class] forCellWithReuseIdentifier:@"cell"];
    [_homeCollectionView release];
    [_flowLayout release];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ZSY_HomeModel *homeModel = [_collectionArray objectAtIndex:indexPath.row];
    self.cell.homeModel = homeModel;
    
    return _cell;
}

// item 的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZSY_HomeModel *model = [_collectionArray objectAtIndex:indexPath.row];
    
    
    [self.detailDelegate pushToDetailView:model.share_url];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _tableArray.count;
}

@end
